<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class AddressController extends Controller
{
    /**
    * Index()进入路由后台首页
    *
    * @return 返回后台首页视图
    */
    public function getIndex(){
    //获取数据表的内容
    $data = DB::table('address')->get();
    //跳转列表页面
      return view('/admin/addresss/list',['data'=> $data]);      
     }

    /**
    * insert()进入路由后台首页
    *
    * @return 返回后台首页视图
    */
    public function getAdd(){
        return view('/admin/addresss/add');
    }
    public function postInsert(Request $request){
        // 获取用户提交信息,并去除掉_token
        $arr = $request -> except(['_token','province','country','town']);
        $a = $request -> only(['province','country','town']);
        $as1 = $request -> only(['province']);
        $as2 = $request -> only(['country']);
        $as3 = $request -> only(['town']);
       foreach ($as1 as $v){
         $arr['province'] =$v;  
       }
        foreach ($as2 as $v){
         $arr['country'] =$v;      
       }
         foreach ($as3 as $v){
         $arr['town'] =$v;         
       }
        $array = implode($a); 
        $arr['address1'] = $array;
        //表内插入信息
        DB::table('address')->insert($arr);
        //跳转到主页
        return redirect('/admin/address/index');
    }
    
     /**
    * delete()进入路由后台删除
    *
    * @return 返回后台首页视图
    */
    public function getDelete(Request $request){
     //获取删除的ID
     $id = $request -> only('id');
     //删除表内的ID
     DB::table('address')->where('id','=',$id)->delete();
    //跳转到主页
     return redirect('/admin/address/index');
    }

    public function getEdit(Request $request){
    //获取id
    $id = $request -> only('id');
    //获取选中的id信息
    $editdata = DB::table('address')->where('id','=',$id)->first();
    //跳转到编辑页
    return view('/admin/addresss/edit',['editdata'=> $editdata]);

    }

    public function postUpdate(Request $request){
       // 获取用户提交信息,并去除掉_token
        $arrs = $request -> except(['_token','province','country','town']);
        $as = $request -> only(['province','country','town']);
        $arrays = implode($as); 
        $as1 = $request -> only(['province']);
        $as2 = $request -> only(['country']);
        $as3 = $request -> only(['town']);
        foreach($as1 as $val1){
           $arrs['province'] = $val1;
        }
        foreach($as2 as $val2){
           $arrs['country'] = $val2;
        }
        foreach($as3 as $val3){
           $arrs['town'] = $val3;
        }
        $arrs['address1'] = $arrays; 
        $res = DB::table('address') -> where('id',$request -> only('id')) -> update($arrs);
        //跳转到主页
        return redirect('/admin/address/index');
   }

#######################前台#################################

    /**
    * Index()进入路由前台首页
    *
    * @return 返回前台首页视图
    */
    public function getIndex1(){
    //获取数据表的内容
    $data = DB::table('address')->get();
    //跳转列表页面
    return  view('home/shopaddr/shopaddress',['data'=> $data]);
    }

    /**
    * insert()进入路由后台插入
    *
    * @return 返回后台首页视图
    */
     public function postInsert1(Request $request){
        // 获取用户提交信息,并去除掉_token
        if(!$request -> has('name')){
            return back() -> withInput() ->with('error','请输入用户名字');
        }
       if(!$request -> has('tel')){
            return back() -> withInput() ->with('errors','请输入手机号');
        }
        $arr = $request -> except(['_token','province','country','town']);
        $a = $request -> only(['province','country','town']);
        $array = implode($a); 
        $arr['address1'] = $array;
        //表内插入信息
        $arrs = DB::table('address')->insert($arr);
        //跳转到主页

        return redirect('/home/shopaddr/index1');
        
    }
    public function getEdit1(Request $request){

    //获取id
    $id = $request -> only('id');

    //获取选中的id信息
    $editdata = DB::table('address')->where('id','=',$id)->first();
    
    return $editdata;
    //跳转到编辑页
    // return redirect('home/shopaddr/index1',['editdata'g=> $editdata]);
    }
    
    public function postUpdate1(Request $request){
       // 获取用户提交信息,并去除掉_token
       $arrs = $request -> except(['_token','provinces','countrys','towns']);
        $as = $request -> only(['provinces','countrys','towns']);
        $as1 = $request -> only(['provinces']);
        $as2 = $request -> only(['countrys']);
        $as3 = $request -> only(['towns']);
        foreach($as1 as $v1){
           $arrs['province'] = $v1;
        }
        foreach($as2 as $v2){
           $arrs['country'] = $v2;
        }
         foreach($as3 as $v3){
           $arrs['town'] = $v3;
        }
        $arrays = implode($as); 
        $arrs['address1'] = $arrays; 
        $res = DB::table('address') -> where('id',$request -> only('uid')) -> update($arrs);
        //跳转到主页
       return redirect('home/shopaddr/index1');
   }
    /**
    * delete()进入路由前台删除
    *
    * @return 返回前台首页视图
    */
    public function getDelete1(Request $request){
     //获取删除的ID
     $id = $request -> only('id');
     //删除表内的ID
     DB::table('address')->where('id','=',$id)->delete();
    //跳转到主页
     return redirect('home/shopaddr/index1');
   }



}
